<template>
  <div class="app-content">
    <h2 class="font-500 mb-20px">上传审核</h2>
    <div v-if="!noAddon" v-loading="loading" class="">
      <div class="text-center m-20px">
        <img :src="defaultApp.logo" class="w-100px rounded-10px" />
        <p>{{ defaultApp.name }} （{{ defaultApp.version || '暂无版本号' }}）</p>
      </div>
      <el-form ref="formRef" :model="form" label-width="80px" class="w-400px m-auto" label-position="top">
        <el-form-item label="版本号">
          <el-input v-model="form.version" />
        </el-form-item>
        <el-form-item label="更新内容">
          <el-input v-model="form.change_log" type="textarea" :autosize="{ minRows: 6, maxRows: 8 }" />
        </el-form-item>
        <div class="text-center">
          <el-button
            v-if="form.version !== defaultApp.version && form.version && !noUpdate"
            class="w-200px"
            type="primary"
            @click="onSubmit()"
          >
            推送上传
          </el-button>
          <template v-if="noUpdate">
            <el-button disabled class="w-200px" type="primary">暂无更新</el-button>
            <el-button type="danger" @click="onSubmitForce()">重新上传</el-button>
          </template>
        </div>
      </el-form>
    </div>
    <div v-if="errorMsg">
      <el-empty :description="errorMsg" />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, inject } from 'vue'
import { useUserStore } from '@/stores/user'
import { getPlaid } from '@/utils/auth'
import { getAddonAppUpdateMsg, updateAddonApp } from '@/api/platform/app/app_upload'
import { ElMessageBox } from 'element-plus'

defineOptions({
  name: 'DkAppUpload'
})

// 响应式状态
const plaid = ref(0)
const formRef = ref(null)
const loading = ref(false)
const defaultApp = inject('defaultApp')
const noUpdate = ref(false)
const noAddon = ref(false)
const form = reactive({})
const errorMsg = ref('')

// 方法
const onSubmitForce = () => {
  ElMessageBox.confirm('当前版本和服务器版本一致，是否提交代码继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      onSubmit()
    })
    .catch(() => {})
}

const onSubmit = () => {
  loading.value = true
  updateAddonApp(defaultApp.value.addon_name, form.change_log, defaultApp.value.id, plaid.value)
    .then((res) => {
      ElMessageBox.alert('推送成功，请到开放平台后台查看并提交审核！', '推送成功')
      loading.value = false
      noUpdate.value = true
    })
    .catch((res) => {
      loading.value = false
      if (!res.data?.error) {
        ElMessageBox.alert(res.msg, '推送失败')
      }
    })
}

// 拉取最新的版本
const getAddonAppMsg = () => {
  loading.value = true
  getAddonAppUpdateMsg(defaultApp.value.addon_name, defaultApp.value.id)
    .then((res) => {
      if (res.msg === 'no_new_version') {
        noUpdate.value = true
      }
      Object.assign(form, res.data.cloud)
      // TODO 这里需要更新defaultApp的值
      loading.value = false
    })
    .catch((res) => {
      noAddon.value = true
      loading.value = false
      errorMsg.value = res.msg
    })
}

// 生命周期钩子
onMounted(() => {
  plaid.value = getPlaid()
  getAddonAppMsg()
})
</script>
